package m.jclecai.com.activity;

import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import m.jclecai.com.R;

import com.easyandroidanimations.library.Animation;
import com.easyandroidanimations.library.BounceAnimation;
import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.bean.ProofBean;
import m.jclecai.com.bean.UserBean;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.utils.BaseUtils;
import m.jclecai.com.utils.MD5;
import m.jclecai.com.utils.MyToast;
import m.jclecai.com.utils.PreferenceUtil;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;
import m.jclecai.com.utils.http.responseBody.BaseBody;

/**
 * Created by ZD on 2017/5/22.
 */

public class UpdatePwdActivity extends BaseActivity {

    @BindView(R.id.et_init_pwd)
    EditText etInitPwd;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.et_rPwd)
    EditText etRPwd;
    @BindView(R.id.ll_init)
    LinearLayout ll_init;
    @BindView(R.id.ll_pwd)
    LinearLayout ll_pwd;
    @BindView(R.id.ll_rPwd)
    LinearLayout ll_rPwd;
    @BindView(R.id.et_auth_code)
    EditText mEtAuthCode;

    @BindView(R.id.tv_send_proof)
    TextView mTvSendProof;

    @BindView(R.id.tv_phone)
    TextView tv_phone;

    @Override
    public int getContentView() {
        return R.layout.activity_update_pwd;
    }

    UserBean userBean = null;
    @Override
    public void initView() {
        super.initView();
        setTitle("修改密码");
        leftOptionText("");
        userBean = PreferenceUtil.readUser(UpdatePwdActivity.this);
        tv_phone.setText(userBean.getPhone());
    }

    @OnClick({R.id.cb_init, R.id.cb_pwd, R.id.cb_rPwd})
    void showPwd(View v) {
        boolean isCheck = ((CheckBox) v).isChecked();
        switch (v.getId()) {
            case R.id.cb_init:
                etInitPwd.setInputType(isCheck ? InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD :
                        (InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT));
                break;
            case R.id.cb_pwd:
                etPwd.setInputType(isCheck ? InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD :
                        (InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT));
                break;
            case R.id.cb_rPwd:
                etRPwd.setInputType(isCheck ? InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD :
                        (InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT));
                break;
        }
    }

    //发送验证码
    @OnClick(R.id.tv_send_proof)
    void sendProof() {

        //这里表示普通绑定手机号的时候发送验证码；
        sendProofCode();
    }

    String url = "";
    @OnClick(R.id.tv_update)
    void updatePwd() {
        String initPwd = etInitPwd.getText().toString();
        final String pwd = etPwd.getText().toString();
        String rPwd = etRPwd.getText().toString();
        String phoneNumberCode = mEtAuthCode.getText().toString().trim();   //手机验证码
//        if(TextUtils.isEmpty(initPwd)){
//            MyToast.showToast(this,"请输入原始密码");
//            new BounceAnimation(ll_init).setNumOfBounces(3)
//                    .setDuration(Animation.DURATION_SHORT).animate();
//            return;
//        }
        if(TextUtils.isEmpty(pwd)){
            MyToast.showToast(this,"请输入新密码");
            new BounceAnimation(ll_pwd).setNumOfBounces(3)
                    .setDuration(Animation.DURATION_SHORT).animate();
            return;
        }
        if (pwd.length() < 4 || pwd.length() > 16) {
            MyToast.showToast(this, "密码不能小于四位或者大于十六位");
            new BounceAnimation(ll_pwd).setNumOfBounces(3)
                    .setDuration(Animation.DURATION_SHORT).animate();
            return;
        }
        if(!pwd.equals(rPwd)){
            MyToast.showToast(this,"两次输入的新密码不匹配");
            new BounceAnimation(ll_rPwd).setNumOfBounces(3)
                    .setDuration(Animation.DURATION_SHORT).animate();
            return;
        }
        if (TextUtils.isEmpty(phoneNumberCode)) {
            MyToast.showToast(this, "请输入手机验证码");
            return;
        }

        showDialog();
//        Map<String,String> pair = new HashMap<>();
//        pair.put("id", userBean.getUser_id());
//        //新密码
//        pair.put("newValue", pwd);
//        //原密码
//        pair.put("password", rPwd);
//        pair.put("vphone", phoneNumberCode);
        url = Constant_Url.UPDATE_PWD
                +userBean.getUser_id()
                +"&newValue="+ MD5.getMD5(pwd)
                +"&password="+ MD5.getMD5(rPwd)
                +"&vphone="+ phoneNumberCode;
        HttpManager.getAsyn(url, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                dissDialog();
                MyToast.showToast(UpdatePwdActivity.this,getString(R.string.error_network));
            }

            @Override
            public void onResponse(String response) {
                dissDialog();
                BaseBody baseBody = FastOk.get(response, BaseBody.class);
                if(baseBody != null) {
                    MyToast.showToast(UpdatePwdActivity.this,baseBody.getMessage());
                    if(baseBody.getStatus() == 0) {
                        PreferenceUtil.clearUserInfo(UpdatePwdActivity.this);
                        setResult(RESULT_OK);
                        finish();
                    }
                }else {
                    MyToast.showToast(UpdatePwdActivity.this,getString(R.string.error_network));
                }
            }
        });
    }




    /**
     * 这里是发送普通的验证码
     */
    private void sendProofCode() {

        String phone = tv_phone.getText().toString().trim();

        BaseUtils.countDowm(UpdatePwdActivity.this, 60, mTvSendProof);
//        HashMap<String, String> pair = new HashMap<>();
//        pair.put("id", userBean.getId());
//        pair.put("phone", phone);
        HttpManager.getAsyn(Constant_Url.SEND_URL+userBean.getUser_id(), new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                MyToast.showToast(UpdatePwdActivity.this, "验证码发送失败");
            }

            @Override
            public void onResponse(String response) {
                ProofBean proofBean = FastOk.get(response, ProofBean.class);
                if (proofBean != null){
                    DisPlay(proofBean.getMessage());
                    if (proofBean.getMessage().contains("不正确")||proofBean.getMessage().contains("对不起")){
                        BaseUtils.CannelTimer(mTvSendProof,"获取验证码");
                    }
                }else{
                    DisPlay("网络异常！！！");
                }
                //MyToast.showToast(UpdatePwdActivity.this, getString(R.string.error_network));

            }
        });

    }
}
